package com.accloud.cloudservice;

import com.accloud.common.ACConfiguration;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACObject;
import com.accloud.service.ACTimerMgr;
import com.accloud.service.ACTimerTask;
import com.accloud.utils.LogUtil;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class ACTimerManager extends ACBaseManager implements ACTimerMgr {
    private TimeZone timeZone;

    public ACTimerManager() {
        this(TimeZone.getDefault());
    }

    public ACTimerManager(TimeZone timeZone2) {
        super(ACConfiguration.TIMER_SERVICE_NAME, 1, false);
        this.timeZone = timeZone2;
    }

    @Override // com.accloud.service.ACTimerMgr
    public void addTask(ACTimerTask.OP_TYPE opType, long deviceId, String name, String timePoint, String timeCycle, ACDeviceMsg msg, final PayloadCallback<ACTimerTask> callback) {
        if (deviceId <= 0 || timePoint.length() <= 0 || timeCycle.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (timeCycle.equals("min")) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "timeCycle can not support min in new version"));
        } else if (!timeCycle.equals("once") && !timeCycle.equals("hour") && !timeCycle.equals("day") && !timeCycle.equals("month") && !timeCycle.equals("year") && !timeCycle.startsWith("week")) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "timeCycle must be one of \"once/hour/day/month/year/week[0，1，2，3，4，5，6]\""));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("addTask");
            if (opType == ACTimerTask.OP_TYPE.CLOUD) {
                req.put("taskFlag", 0);
            } else {
                req.put("taskFlag", 1);
            }
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("timeZone", this.timeZone.getID());
            req.put("timePoint", timePoint);
            req.put("timeCycle", timeCycle);
            req.put("name", name);
            if (msg.getDescription() != null) {
                req.put("description", msg.getDescription());
            }
            req.put("messageCode", Integer.valueOf(msg.getCode()));
            LogUtil.d("sendToDevice msg-->"+msg.toString());
            byte[] payload = msg.getContent();
            if (payload != null) {
                req.setStreamPayload(new ByteArrayInputStream(payload), payload.length);
            } else {
                req.setStreamPayload(null, 0);
            }
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACTimerManager.AnonymousClass1 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    ACObject object = (ACObject) resp.get("task");
                    callback.success(new ACTimerTask(object.getLong("taskId"), object.getString("taskType"), object.getLong("userId"), object.getString("nickName"), object.getString("name"), object.getString("description"), object.getString("timeZone"), object.getString("timePoint"), object.getString("timeCycle"), object.getString("createTime"), object.getString("modifyTime"), (int) object.getLong("status")));
                }
            });
        }
    }

    @Override // com.accloud.service.ACTimerMgr
    public void modifyTask(long deviceId, long taskId, String name, String timePoint, String timeCycle, ACDeviceMsg msg, VoidCallback callback) {
        if (deviceId <= 0 || taskId <= 0 || timePoint.length() <= 0 || timeCycle.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (timeCycle.equals("min")) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "timeCycle can not support min in new version"));
        } else if (!timeCycle.equals("once") && !timeCycle.equals("hour") && !timeCycle.equals("day") && !timeCycle.equals("month") && !timeCycle.equals("year") && !timeCycle.startsWith("week")) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "timeCycle must be one of \"once/hour/day/month/year/week[0，1，2，3，4，5，6]\""));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("modifyTask");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("taskId", Long.valueOf(taskId));
            req.put("timeZone", this.timeZone.getID());
            req.put("timePoint", timePoint);
            req.put("timeCycle", timeCycle);
            req.put("name", name);
            req.put("messageCode", Integer.valueOf(msg.getCode()));
            LogUtil.d("sendToDevice msg-->"+msg.toString());
            if (msg.getDescription() != null) {
                req.put("description", msg.getDescription());
            }
            byte[] payload = msg.getContent();
            if (payload != null) {
                req.setStreamPayload(new ByteArrayInputStream(payload), payload.length);
            } else {
                req.setStreamPayload(null, 0);
            }
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACTimerMgr
    public void openTask(long deviceId, long taskId, VoidCallback callback) {
        if (deviceId <= 0 || taskId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("startTask");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("taskId", Long.valueOf(taskId));
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACTimerMgr
    public void closeTask(long deviceId, long taskId, VoidCallback callback) {
        if (deviceId <= 0 || taskId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("stopTask");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("taskId", Long.valueOf(taskId));
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACTimerMgr
    public void deleteTask(long deviceId, long taskId, VoidCallback callback) {
        if (deviceId <= 0 || taskId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("deleteTask");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("taskId", Long.valueOf(taskId));
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACTimerMgr
    public void listTasks(long deviceId, final PayloadCallback<List<ACTimerTask>> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("listTasks");
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACTimerManager.AnonymousClass2 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    List<ACObject> objects = (List) resp.get("tasks");
                    ArrayList arrayList = new ArrayList();
                    if (objects != null) {
                        for (ACObject object : objects) {
                            arrayList.add(new ACTimerTask(object.getLong("taskId"), object.getString("taskType"), object.getLong("userId"), object.getString("nickName"), object.getString("name"), object.getString("description"), object.getString("timeZone"), object.getString("timePoint"), object.getString("timeCycle"), object.getString("createTime"), object.getString("modifyTime"), (int) object.getLong("status")));
                        }
                    }
                    callback.success(arrayList);
                }
            });
        }
    }
}
